//
//  linear_flight_timer.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 


#ifndef linear_flight_timer_hpp
#define linear_flight_timer_hpp
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include "patterns/command.hpp"
#include "patterns/handlers.hpp"

#include "logging/logging.hpp"

namespace pcmd=patterns::command;
namespace phdl=patterns::handlers;
namespace ba=boost::asio;
namespace bs=boost::system;
namespace bt=boost::posix_time;



namespace components{
namespace utils{



class linear_flight_timer :public boost::enable_shared_from_this<linear_flight_timer>, private boost::noncopyable
{
private:
    std::string name_;

    ba::io_service& ios_master_;

    linear_flight_timer (ba::io_service& ios_master) ;

    ba::deadline_timer timer_;
public:
    typedef boost::shared_ptr<linear_flight_timer> pointer;

    bool cancel () ;

    static boost::shared_ptr<linear_flight_timer> create (ba::io_service& ios_master) ;

    virtual  ~linear_flight_timer () ;

 template<typename Handler >
      bool exec(bt::millisec flight_base, bt::millisec flight_increment, int nitems, Handler& handler) {
        try{
            LOG(INFO)<<name_<<"::"<<__FUNCTION__;
            DLOG(INFO)<<"flight_base:\t"<<flight_base.total_milliseconds();
            DLOG(INFO)<<"flight_increment:\t"<<flight_increment.total_milliseconds();
            DLOG(INFO)<<"nitems:\t"<<nitems ;
            bt::millisec total_wait(flight_base.total_milliseconds()+
                                    nitems*flight_increment.total_milliseconds());
            timer_.expires_from_now(total_wait);
            timer_.async_wait(handler);
            return true;
        }
        catch(std::exception &e){
           LOG(WARNING)<<name_<<"::"<<__FUNCTION__<<":"<<e.what();
           cancel();
           return false;
        }
        
    }
};

}//end namespace utils
}//end namespace components

#endif /*#ifdef linear_flight_timer_hpp*/
